<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RSA 加解密</title>
    <script type="text/javascript" th:src="@{~/js/jquery-2.1.4.min.js}"></script>
    <script type="text/javascript" th:src="@{~/js/jsencrypt.min.js}"></script>
</head>
<body>
<h2>springboot RSA 加密 解密 传参</h2>

<h2>提交参数：</h2>
<input name="name" id="name" value="张三"/></br>
<input name="name" id="age" value="34"/></br>
<input name="name" id="address" value="北京姑娘"/></br>
<input name="name" id="remark" value="测试"/></br>

<input type="button" onclick="btnPostSubmit()" value="post提交"/>
<input type="button" onclick="postPage()" value="postPage"/>

<input type="button" onclick="getById()" value="getById"/>
<input type="button" onclick="getList()" value="getList"/>

<h2>返回参数密文：</h2>
<textarea id="encryptData" rows="10" cols="100"></textarea>
</br>

<h2>返回参数解密：</h2>
<textarea id="decryptData" rows="10" cols="100"></textarea>
</br>

<script type="text/javascript">

    var privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIn2zWqU7K/2qm5pOpq5bp9R+3MTnStWTfJU9nC/Vo7UKH9dITPvrELCTK+qlqpx5Fes+l0GY7n6u4n4jyiw4ejsvkZYQ5ww477yLOn2FcoEGuZEwPgSCmfTST0OFUgQqn+/J11k9L92jEHyieE3qmhMkMt0UsVUSJwx/nZxo30ZAgMBAAECgYBD3YHigeuEC4R+14iaf8jo2j0kuGtB3Cxvnlez0otTqw1YyYkBsU49cLKkXvfKVEgM0Ow/QltgKvSBxCE31PrrDka5TygVMqqA/IM7NrDvjUcGLjyoeNmLA8660fWcDxUTlAGN5kxIvUATayVwKVflpWPWu0FPKsWrZustnEo+4QJBAMCmYsWqAKWYMVRXFP3/XGRfio8DV793TOckyBSN9eh8UhgoZyT3u7oeHmDJEwm4aNMHlg1Pcdc6tNsvi1FRCiUCQQC3VNzfF4xOtUgX7vWPL8YVljLuXmy12iVYmg6ofu9l31nwM9FLQ1TRFglvF5LWrIXTQb07PgGd5DJMAQWGsqLlAkAPE7Z9M73TN+L8b8hDzJ1leZi1cpSGdoa9PEKwYR/SrxAZtefEm+LEQSEtf+8OfrEtetWCeyo0pvKKiOEFXytFAkEAgynL/DC0yXsZYUYtmYvshHU5ayFTVagFICbYZeSrEo+BoUDxdI9vl0fU6A5NmBlGhaZ65G+waG5jLc1tTrlvoQJAXBEoPcBNAosiZHQfYBwHqU6mJ9/ZacJh3MtJzGGebfEwJgtln5b154iANqNWXpySBLvkK+Boq7FYRiD83pqmUg==";
    var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ9s1qlOyv9qpuaTqauW6fUftzE50rVk3yVPZwv1aO1Ch/XSEz76xCwkyvqpaqceRXrPpdBmO5+ruJ+I8osOHo7L5GWEOcMOO+8izp9hXKBBrmRMD4Egpn00k9DhVIEKp/vyddZPS/doxB8onhN6poTJDLdFLFVEicMf52caN9GQIDAQAB";

    $(function () {

        test();

    });

    /**
     * 加密解密测试
     */
    function test() {
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        encrypt.setPrivateKey(privateKey);
        var arr = [];
        for (var i = 1; i < 600; i++) {
            arr.push({
                id: i,
                name: "lisisi" + i,
                age: 12
            });
        }
        var params = {
            total: 100,
            pageSize: 10,
            pageNum: 1,
            data: arr
        };
        alert(JSON.stringify(params).length)
        // 加密  三种方式  encrypt 加密字符长度100   encryptLong 长字符加密 19293  encryptLong2 长字符分段加密  上限值 23193
        var d = encrypt.encryptLong2(JSON.stringify(params));
        $("#encryptData").val("密文：" + d);
        // 解密 三种方式  decrypt  decryptLong  decryptLong2
        var data = encrypt.decryptLong2(d);
        $("#decryptData").val("明文：" + JSON.stringify(data));
    }

    /**
     * 清空数据
     */
    function clearText() {
        $("#encryptData").val("");
        $("#decryptData").val("");
    }

    /**
     * 解析数据
     * @param result
     */
    function processData(result) {
        clearText();
        console.log("返回参数密文：" + JSON.stringify(result));
        $("#encryptData").val("返回参数密文：" + JSON.stringify(result));

        var encrypt = new JSEncrypt();
        encrypt.setPrivateKey(privateKey);
        var data = encrypt.decryptLong(result.data);
        // 解密
        $("#decryptData").val("返回参数解密：" + JSON.stringify(data));
    }

    function btnPostSubmit() {
        var params = {
            name: $("#name").val(),
            age: $("#age").val(),
            address: $("#address").val(),
            remark: $("#remark").val()
        };
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        $.ajax({
            type: "POST",
            url: "/rsaTest",
            data: "{\"requestData\":" + encrypt.encryptLong2(JSON.stringify(params)) + "}",
            dataType: 'json',
            contentType: "application/json",
            success: function (result) {
                processData(result);
            }
        });
    }

    function postPage() {
        var params = {
            pageSize: 10,
            pageNum: 1,
            queryParams: {
                name: $("#name").val(),
                age: $("#age").val(),
                address: $("#address").val(),
                remark: $("#remark").val()
            }
        };
        var encrypt = new JSEncrypt();
        encrypt.setPublicKey(publicKey);
        $.ajax({
            type: "POST",
            url: "/page",
            data: "{\"requestData\":" + encrypt.encryptLong2(JSON.stringify(params)) + "}",
            dataType: 'json',
            contentType: "application/json",
            success: function (result) {
                processData(result);
            }
        });
    }

    function getById() {
        $.ajax({
            type: "get",
            url: "/getById/46789",
            // data: "{\"requestData\":" + encrypt.encrypt(JSON.stringify(params)) + "}",
            dataType: 'json',
            contentType: "application/json",
            success: function (result) {
                processData(result);
            }
        });
    }

    function getList() {
        $.ajax({
            type: "get",
            url: "/list",
            // data: "{\"requestData\":" + encrypt.encrypt(JSON.stringify(params)) + "}",
            dataType: 'json',
            contentType: "application/json",
            success: function (result) {
                processData(result);
            }
        });
    }
</script>
</body>
</html>